/*
 * server_socket.h
 *
 *  Created on: 2021年1月27日
 *      Author: kimfeng
 */

#ifndef SERVER_SOCKET_H_
#define SERVER_SOCKET_H_

#include "connection.h"
#include "connection_manager.h"
#include "io_service_pool.h"
#include <string>
#include <boost/asio.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include "callback_handler.h"

class server_socket: private boost::noncopyable
{
public:
	explicit server_socket(const std::string& address, const std::string& port,
			std::size_t io_service_pool_size, bool reuse_port = false);
	void run();

	void attech_req_func(const ushort req_num, CallbackFunction f)
	{
		m_request_handler.attech_callback_func(req_num, f);
	}
	std::string& get_address() { return m_address; }
	std::string& get_port() { return m_port; }

	boost::asio::io_service& get_io_service();

	connection_ptr get_connection(connection_id_t id);

private:
	  void start_accept();
	  void handle_accept(connection_ptr c, const boost::system::error_code& e);
	  void handle_stop();

	  bool m_reuse_port;
	  std::string m_address;
	  std::string m_port;
	  io_service_pool m_io_svc_pool;
	  boost::asio::signal_set m_signals;
	  boost::asio::ip::tcp::acceptor m_acceptor;
	  connection_ptr m_connection;
	  connection_manager m_manager;
	  callback_handler m_request_handler;

};

#endif /* SERVER_SOCKET_H_ */
